package adminejb;

import java.util.ArrayList;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import repositories.CountryRepository;
import specifications.CountrySpecification;
import entities.Country;

@Stateless
@LocalBean
public class CountryBean implements CountryBeanLocal {

	/**
     * Default constructor. 
     */
	
	public CountryBean() {
	}

	public void createCountry(Country country) {
		CountryRepository cRep = new CountryRepository();
    	cRep.Add(country);
	}

	public void updateCountry(Country country) {
		CountryRepository cRep = new CountryRepository();
    	cRep.Update(country);
	}

	public void deleteCountry(String countrycode) {
		CountryRepository cRep = new CountryRepository();
    	cRep.FindAll();
    	CountrySpecification cs = new CountrySpecification("countryCode", countrycode);
    	ArrayList<Country> list = cRep.findBySpecification(cs);
    	cRep.Delete(list.get(0));
	}

	public ArrayList<Country> getAllCountries() {
		CountryRepository cr = new CountryRepository();
		cr.FindAll();
		return cr.CountryList;
	}

	public Country findCountry(String attr, String value) {
		CountryRepository countryRep = new CountryRepository();
		countryRep.FindAll();
		CountrySpecification cs = new CountrySpecification(attr, value);
    	ArrayList<Country> list = countryRep.findBySpecification(cs);
    	return list.get(0);
	}

}
